package yxy.game.pm2.module.avatar;

import yxy.apple.util.PubSub.MethodSubscriber;
import yxy.apple.util.PubSub.SubscriptionMethod;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.pubsub.ArenaRankPublisher;
import yxy.game.pm2.bean.pubsub.ArenaRankPublisher.ArenaRankSubscription;
import yxy.game.pm2.bean.pubsub.HeroPublisher;
import yxy.game.pm2.bean.pubsub.HeroPublisher.HeroSubscription;
import yxy.game.pm2.bean.pubsub.PlayerBeanPublisher;
import yxy.game.pm2.bean.pubsub.PlayerBeanPublisher.PlayerBeanSubscription;
import yxy.game.pm2.module.avatar.AvatarModule.AvatarCheckType;
import yxy.game.pm2.module.avatar.HeadModule.HeadCheckType;
import yxy.game.pm2.module.avatar.HeadframeModule.HeadframeCheckType;

/** 形象订阅 */
public class AvatarSubscriber extends MethodSubscriber {

	static private AvatarSubscriber instance;

	public static AvatarSubscriber getInstance() {
		if (instance == null) {
			instance = new AvatarSubscriber();
			instance.init();
		}
		return instance;
	}

	private void init() {
		PlayerBeanPublisher.getInstance().subscribe(this);
		HeroPublisher.getInstance().subscribe(this);
		ArenaRankPublisher.getInstance().subscribe(this);
	}

	private AvatarSubscriber() {
	}

	@SubscriptionMethod(Type = PlayerBeanSubscription.class)
	public void receive(PlayerBeanSubscription subscription) {
		PlayerBean playerBean = subscription.getPlayerBean();
//		System.err.println("订阅更新playerBean");
		String playerCode = playerBean.getCode();
		AvatarModule.check(playerCode, AvatarCheckType.玩家等级, playerBean.getLevel());
	}

	@SubscriptionMethod(Type = HeroSubscription.class)
	public void receive(HeroSubscription subscription) {
		Hero hero = subscription.getHero();
//		System.err.println("订阅更新hero");
		String playerCode = subscription.getPlayerCode();
		AvatarModule.check(playerCode, AvatarCheckType.武将星级, hero.getDataId() / 100, hero.getStar());
		HeadModule.check(playerCode, HeadCheckType.武将获取, hero.getDataId() / 100);
	}

	@SubscriptionMethod(Type = ArenaRankSubscription.class)
	public void receive(ArenaRankSubscription subscription) {
		int rank = subscription.getRank();
//		System.err.println("竞技场排名更新Arena");
		String playerCode = subscription.getPlayerCode();
		HeadframeModule.check(playerCode, HeadframeCheckType.竞技场排名, rank);
	}
}
